

//  组件所在页面引用
var _that = null;

//  立即购买回调
var _goBuyFunction = null;

//  添加购物车回调
var _addBuyCarFunction = null;

function init(that, goBuyFunction, addBuyCarFunction) {
  _that = that;
  _goBuyFunction = goBuyFunction;
  _addBuyCarFunction = addBuyCarFunction;

  _that.setData({
    dialogshow: false,
    buycount: 1
  });
}
// 隐藏会话框
function hiddenDialog() {
  _that.setData({
    dialogshow: false
  });
}
// 显示会话框
function showDialog(dtype = 1) {
  _that.setData({
    dialogshow: true,
    dialogtype: dtype
  });
}

// 商品规格选择
function standardSelected(event) {
  var selected = event.currentTarget.dataset.selected;
  var specname = event.currentTarget.dataset.specname;
  var dialogdata = _that.data.dialogdata;
  var item_id=0;
  var standards = dialogdata.standardtypes;
  for (var i = 0; i < standards.length; i++) {
    if (standards[i].name == specname) {
      standards[i].selected = selected;
      standards[i].selectedkey = standards[i].keys[standards[i].vals.indexOf(selected)];
      var valstr = standards[i].pics[standards[i].vals.indexOf(selected)];
      console.log(standards[i].selectedkey);
      if (valstr.indexOf('.jpg') >= 0 || valstr.indexOf('.png') >= 0) {
        dialogdata.iconurl = standards[i].pics[standards[i].vals.indexOf(selected)];
      }
      break;
    }
  }
  var selected_standardtypes = [];
  if (standards.length > 0) {
    var infocheck = true;
    for (var i = 0; i < standards.length; i++) {
      if (standards[i].selectedkey == '') {
        infocheck = false;
        break;
      }
    }
    if (infocheck == true) {
      for (var i = 0; i < standards.length; i++) {
        selected_standardtypes.push(standards[i].selectedkey);
      }
    }
  }
  selected_standardtypes.reverse();
  selected_standardtypes.sort(getApp().compare);
  var itemid = selected_standardtypes.join('_');
  
  var detailsinfo = _that.data.detailsinfo;
  if (_that.data.detailsinfo.spec_price[itemid]) {
    detailsinfo.dprice = _that.data.detailsinfo.spec_price[itemid].price;
    detailsinfo.storage = _that.data.detailsinfo.spec_price[itemid].store_count;
    dialogdata.price = detailsinfo.dprice;
    dialogdata.storage = detailsinfo.storage;
    item_id = _that.data.detailsinfo.spec_price[itemid].item_id;

    var url = '/api/Goods/activity';
    var params = {
      goods_id: _that.data.goods_id,
      item_id: item_id,
      goods_num: 1,
    }
    getApp().request(url, params).then(res => {
      if (res.result && res.result.activity_is_on == 1){
        detailsinfo.oprice = detailsinfo.dprice;
        detailsinfo.dprice = res.result.shop_price;
        dialogdata.price = res.result.shop_price;
        Countown(res.result.start_time, res.result.end_time);
      }
      else{
        cancelCountdown();
      }
      _that.setData({
        detailsinfo: detailsinfo,
        dialogdata: dialogdata
      });
    });
  }
  else{
    _that.setData({
      detailsinfo: detailsinfo,
      dialogdata: dialogdata
    });
  }
}
var interval
function Countown(startT,endT){
  startT = Math.floor((new Date()).getTime() / 1000);
  var totalSecond = endT - startT;
  var dateArr = calTime(totalSecond);
  totalSecond--
  _that.setData({
    countDownDay: dateArr[0],
    countDownHour: dateArr[1],
    countDownMinute: dateArr[2],
    countDownSecond: dateArr[3],
    discountShow: true
  });
  interval = setInterval(function () {
    console.log('interval');
    dateArr = calTime(totalSecond);
    _that.setData({
      countDownDay: dateArr[0],
      countDownHour: dateArr[1],
      countDownMinute: dateArr[2],
      countDownSecond: dateArr[3],
    });
    totalSecond--;
    if (totalSecond < 0) {
      clearInterval(interval);
      wx.showToast({
        title: '活动已结束',
      });
      _that.setData({
        countDownDay: '00',
        countDownHour: '00',
        countDownMinute: '00',
        countDownSecond: '00',
      });
    }
  }, 1000);
}

function calTime(totalSecond){
  // 秒数
  var second = totalSecond;

  // 天数位
  var day = Math.floor(second / 3600 / 24);
  var dayStr = day.toString();
  if (dayStr.length == 1) dayStr = '0' + dayStr;

  // 小时位
  var hr = Math.floor((second - day * 3600 * 24) / 3600);
  var hrStr = hr.toString();
  if (hrStr.length == 1) hrStr = '0' + hrStr;

  // 分钟位
  var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
  var minStr = min.toString();
  if (minStr.length == 1) minStr = '0' + minStr;

  // 秒位
  var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
  var secStr = sec.toString();
  if (secStr.length == 1) secStr = '0' + secStr;

  return [dayStr,hrStr,minStr,secStr];
}

function cancelCountdown(){
  _that.setData({
    discountShow: false
  });
  if(interval){
    clearInterval(interval);
  }
}

// 修改购买数量
function changeBuyCount(event) {
  var status = parseInt(event.currentTarget.dataset.status);
  var cur_status = parseInt(_that.data.buycount);
  cur_status += status;
  if (cur_status > 999) {
    cur_status = 999;
  }
  else if (cur_status <= 0) {
    cur_status = 1;
  }
  _that.setData({
    buycount: cur_status
  });
}
// 输入购买数量
function numInputCheck(event) {
  var inputval = event.detail.value;
  var buycount = parseInt(inputval);
  if (buycount > 999) {
    buycount = 999;
  }
  else if (buycount <= 0) {
    buycount = 1;
  }
  _that.setData({
    buycount: buycount
  });
}
// 数字正则
function reg_check(str) {
  var reg = /^[0-9]*$/;
  if (!reg.test(str)) {
    return false;
  }
  return true;
}
// buy or addbuycar
function buyOrAddCar_action(event) {
  var keystr = event.currentTarget.dataset.dialogtype;
  if (keystr == 1) {
    _goBuyFunction();
  }
  else {
    _addBuyCarFunction();
  }
}
// 导出接口
module.exports = {
  dialogInit: init,
  show: showDialog,
  hide: hiddenDialog,
  standardSelected: standardSelected,
  changeBuyCount: changeBuyCount,
  numInputCheck: numInputCheck,
  buyOrAddCar: buyOrAddCar_action,
  cancelCountdown: cancelCountdown,
  Countown: Countown
}